package com.swan;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class WebSocketController {
    private final SimpMessagingTemplate messagingTemplate;

    public WebSocketController(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @MessageMapping("/send")
    @SendTo
    public String sendMessage(String message) {
        System.out.println("received message: " + message);
        send(message,"swan");
        return message;
    }

    public void send(String message,String user) {
        messagingTemplate.convertAndSendToUser(user,"/queue/message",message);
    }
}
